# tool marcros
#debug := 1
ifdef debug
DBGFLAG := -g -O0
else 
DBGFLAG :=
endif  
CC := g++-7 
CCFLAG := -Wall -std=c++11 -I./p7zip_16.02/CPP/7zip
DBGFLAG := -g
CCOBJFLAG := $(CCFLAG) -c
LINKFLAG := -ldl
# path marcros
BIN_PATH := bin
SRC_PATH := src

# compile marcros
TARGET_NAME := main
ifeq ($(OS),Windows_NT)
	TARGET_NAME := $(addsuffix .exe,$(TARGET_NAME))
endif
TARGET := $(BIN_PATH)/$(TARGET_NAME)
MAIN_SRC := src/main.cpp

# $(warning $(CURDIR))
# $(error "here add the debug info")
DIRS := $(shell ls ./$(SRC_PATH) -R | grep '^\./.*:$$' | awk '{gsub(":","");print}')
Sources := $(foreach dir, $(DIRS), $(wildcard $(dir)/*.c*))
$(warning DIRS=$(DIRS))
$(warning Sources=$(Sources))
OBJS_TMEP := $(patsubst %.cpp,%.o,$(Sources)) 
OBJS := $(addprefix $(BIN_PATH)/, $(addsuffix .o, $(notdir $(basename $(Sources))))) 
# clean files list
DISTCLEAN_LIST := $(OBJS)

CLEAN_LIST := $(TARGET) \
			  $(DISTCLEAN_LIST)

# default rule
default: all

path:
	test -d $(BIN_PATH) || mkdir -p $(BIN_PATH)
modules: $(OBJS_TMEP)
$(OBJS_TMEP):%.o:%.cpp
	$(CC) $(CCOBJFLAG) $(DBGFLAG) $< -o $(BIN_PATH)/$(notdir $@)
objs:$(TARGET)
$(TARGET): $(OBJS)
	$(CC) $(CCFLAG) $(DBGFLAG) $^ -o $@ $(LINKFLAG)

.PHONY: all
all: path modules objs

.PHONY: clean
clean:
	@echo CLEAN $(CLEAN_LIST)
	@rm -f $(CLEAN_LIST)

.PHONY: distclean
distclean:
	@echo CLEAN $(CLEAN_LIST)
	@rm -f $(DISTCLEAN_LIST)